package com.rcache.core.persistence;

import com.rcache.core.Element;
import com.rcache.core.cache.Rcache;
import com.rcache.core.exception.DataPersistenceException;
import com.rcache.core.persistence.strategy.BasePersistenceStrategy;
import com.rcache.core.persistence.utils.FileUtils;

import java.io.*;


public class DataPersistenceImpl extends BasePersistenceStrategy {
    private static DataPersistenceImpl instance;

    private DataPersistenceImpl() {}
    public synchronized static DataPersistenceImpl getInstance(){
        if (instance==null){
            instance=new DataPersistenceImpl();
        }
        return instance;
    }

    @Override
    public boolean put(Rcache cache, Element element) throws DataPersistenceException {
        String newPaht = cache.getDiskCachePath() + "/" + cache.getCacheName();
        try {
            return FileUtils.putFlie(newPaht,element);
        } catch (IOException e) {
            throw new DataPersistenceException(e);
        } catch (ClassNotFoundException e) {
            throw new DataPersistenceException(e);
        }
    }

    @Override
    public boolean replace(Rcache cache, Element element) throws DataPersistenceException {
        String newPaht = cache.getDiskCachePath() + "/" + cache.getCacheName();
        File file = new File(newPaht+element.getObjectKey()+".cache");
        if(file.exists()){
            try {
                FileUtils.putFlie(newPaht,element);
            } catch (IOException e) {
                throw new DataPersistenceException(e);
            } catch (ClassNotFoundException e) {
                throw new DataPersistenceException(e);
            }
            System.out.println("替换成功！");
        }
        return false;
    }

    @Override
    public boolean remove(Rcache cache, Element element) throws DataPersistenceException {
        String newPaht = cache.getDiskCachePath() + "/" +  cache.getCacheName();
        return FileUtils.deleteFile(newPaht+element.getObjectKey()+".cache");
    }

    @Override
    public Element get(Rcache cache, Object ElementKey) throws DataPersistenceException {
        String newPaht = cache.getDiskCachePath() + "/" + cache.getCacheName();
        try {
            return FileUtils.getFile(newPaht,ElementKey);
        } catch (IOException e) {
            throw new DataPersistenceException(e);
        } catch (ClassNotFoundException e) {
            throw new DataPersistenceException(e);
        }
    }

    @Override
    public Integer getSize(Rcache cache) throws DataPersistenceException {
                String newPaht = cache.getDiskCachePath() + "/" + cache.getCacheName();
        return  FileUtils.numberOfFiles(newPaht);
    }



    @Override
    public boolean tryRestoreFromLocal(Rcache cache) throws DataPersistenceException {
        String newPaht = cache.getDiskCachePath() + "/" + cache.getCacheName();
        try {
            return FileUtils.tryRestore(newPaht,cache);
        } catch (IOException e) {
            throw new DataPersistenceException(e);
        } catch (ClassNotFoundException e) {
            throw new DataPersistenceException(e);
        }
    }




}
